<?php
require_once dirname(__FILE__)."/../UserService.php";
require_once dirname(__FILE__)."/../../ApplicationException.php";
require_once dirname(__FILE__)."/../../model/User.php";

class UserServiceImpl implements UserService {

    public function __construct() {
        $this->userDAO = new UserSQLDAOImpl();
    }

    private $userDAO;

    public function add(User $user) {
        try {
            $this->userDAO->create($user);
        } catch (DataAccessException $e) {
            throw new ApplicationException($e->getMessage());
        }
    }

    public function getById($id) {
        try {
            return $this->userDAO->read($id);
        } catch (DataAccessException $e) {
            throw new ApplicationException($e->getMessage());
        }
    }

    public function getByUid($uid) {
        try {
            return $this->userDAO->getByUid($uid);
        } catch (DataAccessException $e) {
            throw new ApplicationException($e->getMessage());
        }
    }

    public function update(User $user) {
        try {
            $this->userDAO->update($user);
        } catch (DataAccessException $e) {
            throw new ApplicationException($e->getMessage());
        }
    }

    public function deleteById($id) {
        try {
            $this->userDAO->delete($id);
        } catch (DataAccessException $e) {
            throw new ApplicationException($e->getMessage());
        }
    }

    public function checkIfRegistered($uid) {
        $isRegistered = false;
        $count = $this->userDAO->countUid($uid);
        if ($count >= 1) {
            $isRegistered = true;
        }
        return $isRegistered;
    }

    public function checkIfUserNameIsAvailable($userName) {
        $isAvailable = false;
        $count = $this->userDAO->countUserName($userName);
        if ($count < 1) {
            $isAvailable = true;
        }
        return $isAvailable;
    }

}
?>
